package cc.rengu.igas.share.core.service.trans;

import cc.rengu.igas.share.common.constant.ShareServiceConstant;
import cc.rengu.igas.share.common.util.GlobalEsbUtil;
import cc.rengu.igas.smps.facade.base.Header;
import cc.rengu.igas.smps.facade.request.AlarmNoticeRequest;
import cc.rengu.igas.smps.facade.request.GeneralSmsNoticeRequest;
import cc.rengu.igas.smps.facade.response.AlarmNoticeResponse;
import cc.rengu.igas.smps.facade.response.GeneralSmsNoticeResponse;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;

public class RiskMonitorAlarmService extends DubboAsynBaseService {
    private static final String RISK_PUSH_PARAM = "RISK_PUSH_PARAM";
    private static final String ALARM_PUSH_PHONE = "alarmPushPhone";

    @BIZ_PROCESS
    public void proceAlarmPlatResult() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String responseStr = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.DUBBO_RESPONSE);
        Result<AlarmNoticeResponse> result = JSON.parseObject(responseStr, new TypeReference<Result<AlarmNoticeResponse>>() {
        });
        if (null == result || !result.isSuccess() || !OltpRpcdEnum.TRANS_SUCCESS.getRespCode().equals(result.getResult().getRespCode())) {
            /* 获取发送短信的手机号 */
            AlarmNoticeRequest alarmPlatformNoticeRequest = (AlarmNoticeRequest) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_REQUEST, AlarmNoticeRequest.class);
            SysParamService sysParamService = new SysParamServiceImpl();
            SysParam sysParam = sysParamService.getSysParamInfo(alarmPlatformNoticeRequest.getHeader().getInstId(), RISK_PUSH_PARAM, ALARM_PUSH_PHONE);
            if (null == sysParam) {
                sysParam = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID, RISK_PUSH_PARAM, ALARM_PUSH_PHONE);
            }
            if (null != sysParam && !StringUtil.isEmptyOrNull(sysParam.getParamValue().trim())) {
                /*调用告警平台失败，发送短信给告警负责人 */
                DubboService dubboService = new DubboServiceImpl();
                Header header = new Header();
                header.setVersion(alarmPlatformNoticeRequest.getHeader().getVersion());
                header.setTransDate(alarmPlatformNoticeRequest.getHeader().getTransDate());
                header.setTransTime(alarmPlatformNoticeRequest.getHeader().getTransTime());
                header.setTxnNum("00450000000E01");
                header.setBizType(AppParamConstant.DEFAULT_BIZ_TYPE);
                header.setInstId(alarmPlatformNoticeRequest.getHeader().getInstId());
                header.setSrcSysId(AppParamConstant.SYS_ID);
                header.setChanlId(AppParamConstant.SYS_ID);
                header.setTraceNo(GlobalEsbUtil.generateEsbSeqeuenceNumber(alarmPlatformNoticeRequest.getHeader().getInstId()));
                GeneralSmsNoticeRequest generalSMSNoticeRequest = new GeneralSmsNoticeRequest();
                generalSMSNoticeRequest.setHeader(header);
                generalSMSNoticeRequest.setRealtimeFlag(AppParamConstant.YES);
                generalSMSNoticeRequest.setAppointTime("0");
                List<String> phoneNoList = new ArrayList<>();
                phoneNoList.add(sysParam.getParamValue().trim());
                generalSMSNoticeRequest.setPhoneList(phoneNoList);
                generalSMSNoticeRequest.setTemplateId("X01");
                generalSMSNoticeRequest.setSmsContent(JSON.toJSONString(alarmPlatformNoticeRequest));
                Result<GeneralSmsNoticeResponse> smsResult = (Result<GeneralSmsNoticeResponse>) dubboService.callDubboService(ShareServiceConstant.SMS_PLAT_SEND_SERVICE, ShareServiceConstant.SMS_PLAT_SEND_METHOD, generalSMSNoticeRequest);
                if (null == smsResult || !smsResult.isSuccess() || !OltpRpcdEnum.TRANS_SUCCESS.getRespCode().equals(smsResult.getResult().getRespCode())) {
                    rglog.error("调用短信平台发送告警信息失败:{}", JSON.toJSONString(generalSMSNoticeRequest));
                }
            }
        }
    }
}
